﻿using System.Configuration;
using System.Data;
using System.Data.SqlClient;

using asscope.configuration;

namespace asscope.core.Services
{
    public static class SQLHelper
    {
        public static DataSet ExecuteDataset(SqlCommand sqlCommand)
        {
            DataSet dataSet1;
            string connectionString = asscope.configuration.ConfigManager.getConnectionString();
            using (var sqlConnection = new SqlConnection(connectionString))
            {
                try
                {
                    sqlConnection.Open();
                    sqlCommand.Connection = sqlConnection;
                    var sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                    var dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet);
                    dataSet1 = dataSet;
                }
                catch (SqlException e)
                {
                    throw (e);
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
            return dataSet1;
        }

        public static int ExecuteNonQuery(SqlCommand sqlCommand)
        {
            return ExecuteNonQuery(null, sqlCommand);
        }

        public static int ExecuteNonQuery(SqlTransaction sqlTransaction, SqlCommand sqlCommand)
        {
            bool flag;
            int i;

            var connectionString = asscope.configuration.ConfigManager.getConnectionString();
            var sqlConnection = new SqlConnection(connectionString);
            try
            {
                flag = sqlTransaction == null;
                if (!flag)
                {
                    sqlCommand.Transaction = sqlTransaction;
                    sqlCommand.Connection = sqlTransaction.Connection;
                }
                else
                {
                    sqlConnection.Open();
                    sqlCommand.Connection = sqlConnection;
                }
                i = sqlCommand.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw (e);
            }
            finally
            {
                flag = sqlConnection.State != ConnectionState.Open;
                if (!flag)
                    sqlConnection.Close();
            }
            return i;
        }

        public static SqlDataReader ExecuteReader(SqlCommand sqlCommand)
        {
            try
            {
                var connectionString = asscope.configuration.ConfigManager.getConnectionString();
                using (var sqlConnection = new SqlConnection(connectionString))
                {
                    sqlConnection.Open();
                    sqlCommand.Connection = sqlConnection;
                }
                using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    return sqlDataReader;
                }
            }
            catch (SqlException e)
            {
                throw (e);
            }
        }

        public static string ExecuteScalar(SqlCommand sqlCommand)
        {
            string s;
            var connectionString = asscope.configuration.ConfigManager.getConnectionString();
            var sqlConnection = new SqlConnection(connectionString);
            try
            {
                sqlConnection.Open();
                sqlCommand.Connection = sqlConnection;
                var obj = sqlCommand.ExecuteScalar();
                s = obj == null ? string.Empty : obj.ToString();
            }
            catch (SqlException e)
            {
                throw (e);
            }
            finally
            {
                var flag = sqlConnection.State != ConnectionState.Open;
                if (!flag)
                    sqlConnection.Close();
            }
            return s;
        }
    }
}